-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[utils] Allow passing NaN
as defaultValue
to useControlled
#41559
Conversation
Netlify deploy previewhttps://deploy-preview-41559--material-ui.netlify.app/ Bundle size report |
NaN
as defaultValue to useControlled
NaN
as defaultValue
to useControlled
Just curious - are all changes supposed to go into |
it('should not raise a warning if setting NaN as the defaultValue when uncontrolled', () => { | ||
expect(() => { | ||
render(<TestComponent defaultValue={NaN}>{() => null}</TestComponent>); | ||
}).not.toErrorDev(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should it support this? It looks like more bundle size and complexity for no clear value in exchange.
Object.is is not supported in IE11
We don't support IE 11 anymore, so maybe Object.is
would make it, a light DX improvement for no real UX cost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example is if you have a Select
component where one of the options has value of NaN
and you set the defaultValue
to NaN
you would incorrectly get the "component is changing the default value state" warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bundle size shouldn’t be impacted at all because the whole effect is wrapped in a process.env.NODE_ENV
check, and nowhere else in the codebase uses Object.is
and there are IE11
comments, so we figured it’s better to keep the partial IE11 support but more than happy to use Object.is
(that’s what React uses for dependency arrays, for example)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example is if you have a Select component where one of the options has value of
NaN
Isn't it wrong that an option has a value of NaN
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example is if you have a Select component where one of the options has value of
NaN
Isn't it wrong that an option has a value of
NaN
?
Respectfully, I don't think there's anything wrong with using NaN
as a value. For example I could use it to represent a "None" value, or maybe the number value comes from user input and NaN
represents an invalid input.
94adbb8
to
a330197
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide a reproduction? This StackBlitz sandbox template may be a good starting point.
Simple repro with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine to me, but since it's also used in MUI X, it should be reviewed by others too. cc @flaviendelangle @Janpot (Tagging Jan because it might be used in Toolpad too).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no problems with this, the warning is about comparing values of props between renders and React also use Object.is
for this purpose. It doesn't affect production bundle sizes.
Doesn't mean I think it's a good idea to use NaN
as a value to represent state, but it should probably be left to the consumer of this hook to decide how to handle it.
I agree 👍 generally we are moving towards using |
Completely contrived example but you could imagine an educational calculator component that lets you pick from values like NaN, -10, 10 to see the impact of multiplying it by another hard-coded number. Sure, you could write code to treat null as NaN, but seems a bit annoying 😛 |
@mj12albert Since you approved it, could you merge it? Let's not leave it hanging, unless you're waiting for something specific. Should we also consider cherry-picking this to the |
Just occurred to me to quickly test this out: <Select defaultValue={NaN}>
<MenuItem value={NaN}>NaN</MenuItem>
</Select>
and
Though this doesn't necessarily block the PR, just wanted to ask @iammminzzy if you tried these changes with any (other) Material UI component successfully! |
@iammminzzy Any updates on #41559 (comment)? |
The stackblitz link in this comment uses an AutoComplete component which doesn't throw these warnings with NaN value #41559 (comment) Personally I think we should "fix" the other two warnings as well but that's unrelated to this PR. |
It's the same repro as @QingqiShi shared above. |
It looks good 👍 |
On making this work: <Select defaultValue={NaN}>
<MenuItem value={NaN}>NaN</MenuItem>
</Select> I think it's important to understand what So from, there, we can check why the IEEE-754 committee made it so that
So I think that |
…#41559) Co-authored-by: ZeeshanTamboli <zeeshan.tamboli@gmail.com>
This fixes an issue with a warning getting logged when passing
NaN
asdefaultValue
touseControlled
due to using===
instead ofObject.is
. However,Object.is
is not supported in IE11 so I made the change in a backwards-compatible way.